package can;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;

import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class SearchBean {
	private String results = "";
	private ArrayList<Element> resultsList;  //list to be populated in the future with Elements that are to be displayed in search.xhtml

	// input fields
	private String query;
	private ArrayList<ArrayList<Option>> options;
	private ArrayList<Option> nats;
	private ArrayList<Option> years;
	private ArrayList<Option> corps;
	private ArrayList<Option> types;

	// constructor
	public SearchBean() {
		options = new ArrayList<ArrayList<Option>>();
		nats = new ArrayList<Option>();
		years = new ArrayList<Option>();
		corps = new ArrayList<Option>();
		types = new ArrayList<Option>();
		setParams();
	}

	// action method - does the operation
	public void doSearch() {
		System.out.println("HEY");
		
		// parse all fields and construct the operation command
		String optionString = "$";
		for(int i = 0; i < options.size(); i++){
				optionString += options.get(i) + "$";
		}

		// call RequestHandler with proper parameters
		results = RequestHandler.search(query, optionString);
		System.out.println(results);
		
		resultsList = new ArrayList<Element>();
				
		int prevBound = 0;
		int nextBound = 0;
		
		for(int i=0; i < results.length(); i++ ){
			if(results.charAt(i) == '$'){
				nextBound = i;
                Element e = new Element(results.substring(prevBound, nextBound));
                resultsList.add(e);
				prevBound = i+1;
			}
		}
		
		
		//this will change results to a string that is pretty and display-able
//		String tempResults = "";
//		
//		for(int i = 0; i < results.length(); i++){
//			
//			if(results.charAt(i) == ('$')){
//				tempResults += "\n";
//			}
//			
//			else {
//				tempResults += results.charAt(i);
//			}
//		}
//		
//		results = tempResults;
		
		
	}

	// Setters and getters
	public void setParams() {
		// CHANGE ME MATT
		nats.add(new Option("NA", "nationality"));
		nats.add(new Option("EU", "nationality", false));
		years.add(new Option("y2010", "year"));
		years.add(new Option("y2011", "year"));
		years.add(new Option("y2012", "year"));
		corps.add(new Option("GE", "company"));
		corps.add(new Option("Oracle", "company"));
		types.add(new Option("MRI", "doctype"));
		types.add(new Option("DISCHARGE", "doctype"));
		options.add(nats);
		options.add(years);
		options.add(corps);
		options.add(types);
	}

	public String getResults() {
		return results;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public String getQuery() {
		return query;
	}
	
	public void printQuery() {
		System.out.println(query);
	}

	public ArrayList<ArrayList<Option>> getOptions() {
		return options;
	}

	public void setOptions(ArrayList<ArrayList<Option>> options) {
		this.options = options;
	}

	public ArrayList<Option> getNats() { 
		return nats;
	}

	public void setNats(ArrayList<Option> nats) {
		this.nats = nats;
	}

	public ArrayList<Option> getYears() {
		return years;
	}

	public void setYears(ArrayList<Option> years) {
		this.years = years;
	}

	public ArrayList<Option> getCorps() {
		return corps;
	}

	public void setCorps(ArrayList<Option> corps) {
		this.corps = corps;
	}

	public ArrayList<Option> getTypes() {
		return types;
	}

	public void setTypes(ArrayList<Option> types) {
		this.types = types;
	}
	
	public void setResultsList(ArrayList<Element> resultsList) {
		this.resultsList = resultsList;
	}
	
	public ArrayList<Element> getResultsList(){
		return resultsList;
	}

	

}
